package com.originskyseed.platform.cms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.cms.mapper.GiftRelateReceiveMapper;
import com.originskyseed.platform.cms.model.AnchorRelatePlayerVO;
import com.originskyseed.platform.cms.model.GiftRelateReceive;
import com.originskyseed.platform.cms.service.IGiftRelateReceiveService;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.mapper.DeptMapper;
import com.originskyseed.platform.user.model.Dept;

/**
 * 礼品领取信息Service接口实现类（OSI_GIFT_RELATE_RECEIVE）
 *
 * @author WuZhuoXi
 */
@DubboService
@Component("giftRelateReceiveService")
public class GiftRelateReceiveServiceImpl implements IGiftRelateReceiveService {

	@Resource
	private GiftRelateReceiveMapper giftRelateReceiveMapper;

	@Resource
	private DeptMapper deptMapper;

	@Resource
	private IGiftRelateReceiveService giftRelateReceiveService;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.giftRelateReceiveMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.giftRelateReceiveMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(GiftRelateReceive record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.giftRelateReceiveMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(GiftRelateReceive record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.giftRelateReceiveMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(GiftRelateReceive record) throws Exception {
		record.setUpdateTime(new Date());
		return this.giftRelateReceiveMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(GiftRelateReceive record) throws Exception {
		record.setUpdateTime(new Date());
		return this.giftRelateReceiveMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public GiftRelateReceive selectByPrimaryKey(String id) throws Exception {
		return this.giftRelateReceiveMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<GiftRelateReceive> selectList(GiftRelateReceive record) throws Exception {
		return this.giftRelateReceiveMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<GiftRelateReceive> selectListByPage(Map<String, Object> params, BasePage<GiftRelateReceive> page) throws Exception {
		params.put("page", page);
		List<String> deptIdList = getDeptIdList(params);
		params.put("deptIdList", deptIdList);
		List<GiftRelateReceive> list = this.giftRelateReceiveMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 主播维护玩家情况记录列表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<AnchorRelatePlayerVO> selectAnchorRelatePlayerListByPage(Map<String, Object> params, BasePage<AnchorRelatePlayerVO> page) throws Exception {
		params.put("page", page);
		List<AnchorRelatePlayerVO> list = this.giftRelateReceiveMapper.selectAnchorRelatePlayerListByPage(params);
		/*if(list != null && list.size() > 0){
			for (AnchorRelatePlayerVO anchorRelatePlayerVO : list) {
				if(anchorRelatePlayerVO.getPlayerId() != null && StringUtils.isNotBlank(anchorRelatePlayerVO.getPlayerId())){
					GiftRelateReceive giftRelateReceive = new GiftRelateReceive();
					giftRelateReceive.setPlayerId(anchorRelatePlayerVO.getPlayerId());
					List<GiftRelateReceive> giftRelateReceiveList = this.giftRelateReceiveService.selectList(giftRelateReceive);
					anchorRelatePlayerVO.setGiftRelateReceiveList(giftRelateReceiveList);
				}
			}
		}*/
		page.setResults(list);
		return page;
	}

	/**
	 * 获取部门id集合
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	private List<String> getDeptIdList(Map<String, Object> params) throws Exception {
		List<String> deptIdList = new ArrayList<>();
		if (params.containsKey("deptId")) {
			String deptId = params.get("deptId") == null ? "" : params.get("deptId").toString();
			if (deptId == null || StringUtils.isBlank(deptId)) {
				return deptIdList;
			}
			Dept dept = deptMapper.selectByPrimaryKey(deptId);
			String deptCode = dept == null ? "" : dept.getCode();
			List<Dept> deptList = new ArrayList<>();
			getAllDept(deptList, deptCode);
			for (int i = 0; i < deptList.size(); i++) {
				deptIdList.add(deptList.get(i).getId());
			}
		}
		return deptIdList;
	}

	/**
	 *
	 * @param deptCode
	 * @return
	 * @throws Exception
	 */
	private List<Dept> getAllDept(List<Dept> deptList, String deptCode) throws Exception {
		if (deptCode == null || StringUtils.isEmpty(deptCode)) {
			return deptList;
		} else {
			List<Dept> tempDeptList = deptMapper.selectByParentCode(deptCode);
			List<Dept> tempDeptList0 = new ArrayList<>();
			if (tempDeptList != null && tempDeptList.size() > 0) {
				for (int i = 0; i < tempDeptList.size(); i++) {
					Dept tempDept = tempDeptList.get(i);
					String code = tempDept.getCode();
					List<Dept> tempDeptList1 = deptMapper.selectByParentCode(code);
					if (tempDeptList1 == null || tempDeptList1.size() == 0) {
						tempDeptList0.add(tempDept);
					} else {
						getAllDept(deptList, code);
					}
				}
				deptList.addAll(tempDeptList0);
			} else {
				Dept tempDept = deptMapper.selectByCode(deptCode);
				if (tempDept != null) {
					deptList.add(tempDept);
				}
			}
		}
		return deptList;
	}

	/**
	 * 导出数据Excel
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<GiftRelateReceive> selectListToExcel(Map<String, Object> params) throws Exception {
		return this.giftRelateReceiveMapper.selectListByPage(params);
	}

}
